<template>
	<view class="main">
		<u-sticky>
			<view class="top-warp" id='top-warp'>

				<view class="card">
					<view class="card-left">
						<view class="card-item">
							<view class="item-title">
								<text>销售数量</text>
							</view>
							<view class="item-value">
								{{displayBoolen ? statsCommodityStockRecord.totalQuantity || 0 : "******"}}
							</view>
						</view>
						<view class="card-item">
							<view class="item-title">
								<text>利润总额</text>
							</view>
							<view class="item-value">
								{{displayBoolen ?profit.toFixed(2) || 0 : "******"}}
							</view>
						</view>
					</view>
					<!-- <view class="card-right">
						<view class="display" @click="displaySwitch">
							<u-icon v-if="displayBoolen" size="26rpx" color="#fff" name="eye"></u-icon>
							<u-icon v-if="!displayBoolen" size="26rpx" color="#fff" name="eye-off"></u-icon>
						</view>
					</view> -->
				</view>
				<!-- 自定义时间 -->
				<view class="u-m-t-20">
					<headData ref="hedeDataRef" :param="param" @params="getConfirmDate">
					</headData>
				</view>
				<!-- 选择，今天、昨天、本月、上月、今年 -->
				<view class="business-hotspot">
					<view v-for="(item, index) in dateStrSelectList" :key="index">
						<view class="dflex-c" :class="item.value == inds ? 'tag-sel' : 'tag'"
							@click="dateStrOpt(item.value)">
							{{item.name}}
						</view>
						<view :class="item.value == inds ? 'slider' : 'tag'"></view>
					</view>
				</view>

				<!-- 选场地 -->
				<view class="header-sort-site-view">
					<view class="header-sort-site" @click="showSiteTitle()">
						<text class="title">{{param.siteTitle == '' ? '全部场地' : param.siteTitle}}</text>
						<u-icon v-if="!siteChecksShow" name="arrow-down-fill" color="#7D7D7F" size="10"
							class="header-sort-site-icon"></u-icon>
						<u-icon v-if="siteChecksShow" name="arrow-up-fill" color="#7D7D7F" size="10"
							class="header-sort-site-icon"></u-icon>
					</view>
					<view class="header-sort-site">
						<text class="a" v-if="router == 1" @click="clickRouter(2)">记录明细</text>
						<text class="a" v-if="router == 2" @click="clickRouter(1)">排行榜</text>
					</view>
				</view>
				<merchantStoreSearchCheckbox ref="merchantStoreSearchCheckbox" :isShowToIn="siteChecksShow"
					@hideShow="closeSiteTitle()" @setSiteList="setSiteList" />

				<view v-if="commodity.commodityId">
					<view class="ranking-item-out">
						<view class="item-limit">
							<view v-if="commodity.top == 1 || commodity.top == 2 || commodity.top == 3">
								<u--image :showLoading="true"
									:src="staticFileUrl+'/image/stats/top'+commodity.top +'.png'" width="48rpx"
									height="57rpx" radius="10rpx"></u--image>
							</view>
							<view v-else>
								{{commodity.top}}
							</view>
						</view>
						<view class="item-left">
							<u--image :showLoading="true" :src="imgURL + '/' + commodity.goodsFileName" width="80rpx"
								height="80rpx" radius="10rpx"></u--image>
						</view>
						<view class="item-right">
							<view class="item-info">
								<view class="item-name">
									{{commodity.goodsName}}
								</view>
								<view class="item-m">
									条码: {{commodity.goodsBarCode}}
								</view>
							</view>
							<view class="item-count">
								￥{{commodity.totalCostPrice}}
							</view>
							<view class="item-count">
								{{commodity.totalQuantity}}个
							</view>
						</view>
						<view class="close-icon" @click="closeIcon">
							<u-icon name="close-circle" color="red" size="40rpx"></u-icon>
						</view>
					</view>
				</view>
			</view>
		</u-sticky>
		<!-- <scroll-view scroll-y :style="[{height: height + 'px'}]" lower-threshold="200" @scrolltolower="reachBottom"> -->
		<view v-show="router == 1">
			<!-- 排行榜 -->
			<vending-machine-list-stats ref="vendingMachineListStats" :height="height" :endDateStr="param.endDateStr"
				:startDateStr="param.startDateStr" :merchantStoreIdArray="mchStoreIdArray" :dayType="inds"
				:secondaryType="secondaryType" :firstType="firstType" :num="num" @rankingClick="rankingClick">
			</vending-machine-list-stats>
		</view>
		<view v-show="router == 2">
			<view v-if="list.length > 0">
				<view v-for="(item, index) in list" :key="index">
					<view class="card-list">
						<view class="shuoming">
							<view class="title">{{ item.goodsName }}</view>
							<view class="yuan">{{ item.quantity}}个</view>
						</view>
						<view class="time u-flex-row u-row-between">
							<view>{{item.storeName}}</view>
							<view class="device-info">
								<text v-if="item.deviceAlias">{{item.deviceAlias}}号_</text>
								<text v-if="item.deviceTypeName">{{item.deviceTypeName}}_</text>
								<text v-if="item.deviceNo">{{item.deviceNo}}_</text>
								<text v-if="item.hierarchy && item.freightLane">
									{{item.hierarchy}}{{item.freightLane>9?item.freightLane:'0'+item.freightLane}}
								</text>
								<text v-if="item.remarkName">（{{item.remarkName}}）</text>
							</view>
						</view>
						<view class="time u-flex-row u-row-between">
							<view>{{Date.parse(item.createTime) | date('yyyy/mm/dd hh:MM:ss')}}</view>
						</view>
					</view>

				</view>
			</view>
			<u-empty v-else text="暂无数据" mode="list" icon="http://cdn.uviewui.com/uview/empty/list.png" />
			<u-loadmore v-if="list.length > 0" :status="loadmoreStatus" :loading-text="loadingText"
				:loadmore-text="loadmoreText" :nomore-text="nomoreText" height="70rpx" line=true>
			</u-loadmore>
		</view>
	</view>
</template>

<script>
	import headData from '@/components/head-data/head-data.vue'
	import merchantStoreSearchCheckbox from '@/components/merchant-store-search-checkbox/merchant-store-search-checkbox.vue'
	import vendingMachineListStats from '@/components/vending-machine-list-stats/vending-machine-list-stats.vue'
	export default {
		components: {
			headData,
			merchantStoreSearchCheckbox,
			vendingMachineListStats
		},
		data() {
			return {
				height: 0,
				inds: 0,
				operateTypeEnum: this.$operateTypeEnum,
				param: {
					startDateStr: '',
					endDateStr: '',
					siteTitle: '',
					dateType: 1,
				},
				title: '',
				displayBoolen: true,
				current: 0,
				showDay: false,
				showMonth: false,
				showCashPopup: false,
				siteChecksShow: false,
				profit:0,
				staticFileUrl: this.$configPath.staticFileUrl,
				imgURL: this.$configPath.imgURL,
				dateTime: {
					startYMD: '',
					endYMD: '',
					startYM: '',
					endYM: '',
				},
				operationObj: {
					giftConsume: 0,
				},
				channelTypeEnum: this.$channelTypeEnum,
				dateStrSelectList: [{
						value: 0,
						name: '今天'
					},
					{
						value: 1,
						name: '昨天'
					},
					{
						value: 3,
						name: '本月'
					},
					{
						value: 5,
						name: '上月'
					},
					{
						value: 4,
						name: '今年'
					}
				],
				mchStoreIdArray: [

				],

				loadmoreStatus: 'loadmore',
				loadingText: '努力加载中',
				loadmoreText: '轻轻上拉',
				nomoreText: '实在没有了',

				params: {
					pageNum: 1,
					pageSize: 10,
					totalPage: '',
				},
				list: [],

				// 1 = 排行榜, 2 = 明细
				router: 1,
				//售货机销售出库单
				secondaryType: this.$commodityStockRecordSecondTypeEnum.VENDING_MACHINE_OUT,
				firstType: this.$commodityStockRecordFirstTypeEnum.OUTBOUND,
				// 统计对象
				statsCommodityStockRecord: {
					totalRetailPrice:0,
					totalCostPrice:0,
					totalQuantity:0,
				},

				// 商品对象
				commodity: {}
			}
		},
		mounted() {
			let query = wx.createSelectorQuery().in(this)
			query.select('#top-warp').boundingClientRect()
			query.exec(res => {
				let windowHeight = uni.getSystemInfoSync().windowHeight;
				this.height = windowHeight - 50;
				for (let i = 0; i < res.length; i++) {
					this.height -= res[i].height;
				}
				console.log("底部导航栏高度", this.height)
			})
		},
		
		methods: {
			ReachBottom(){
				console.log("滚动了吗")
				if (this.router == 2) {
					if (this.loadmoreStatus == "loading" || this.loadmoreStatus == "nomore") {
						return;
					}
					this.loadmoreStatus = "loading";
					this.params.pageNum += 1;
					this.findCommodityStockRecordList();
				}
			},
			Ready() {
				this.$refs.hedeDataRef.timesTempMax = Number(this.$date.getEndDate())
			},
			Load() {
				this.param.startDateStr = this.$date.dateFormatYMD(new Date());
				this.param.endDateStr = this.$date.dateFormatYMD(new Date());
				this.initData();
			},
			//初始化数据
			initData() {
				this.params.pageNum = 1;
				this.list = [];
				this.findStatsCommodityStockRecordSum()
				if (this.router == 1) {
					uni.showLoading({
						mask: true,
						title: '加载中'
					})
					this.$nextTick(() => {
						//查询排行榜
						this.$refs.vendingMachineListStats.reload();
					})
				} else if (this.router == 2) {
					//查询明细
					this.findCommodityStockRecordList();
				}
			},
			//头部统计
			findStatsCommodityStockRecordSum() {
				let params = {
					startDateStr: this.param.startDateStr,
					endDateStr: this.param.endDateStr,
					merchantStoreIdArray: this.mchStoreIdArray,
					dayType: this.inds,
					secondaryType: this.secondaryType,
					firstType: this.firstType
				}
				console.log("参数",params)
				this.$api('statsCommodityStockRecord.findStatsCommodityStockRecordSum', params).then(res => {
					if (res.status == 0) {
						if(res.data != null){
							this.statsCommodityStockRecord = res.data;
						}else{
							this.statsCommodityStockRecord.totalRetailPrice = 0 
							this.statsCommodityStockRecord.totalCostPrice = 0
							this.statsCommodityStockRecord.totalQuantity = 0
						}
						this.profit = this.statsCommodityStockRecord.totalRetailPrice - this.statsCommodityStockRecord.totalCostPrice
					} else {
						this.$helpers.toast('请求数据失败:' + res.msg);
					}
				}).catch(res => {
					this.$helpers.toast('服务器访问失败');
				})
			},
			findCommodityStockRecordList() {
				let params = {
					startTime: this.param.startDateStr + ' 00:00:00',
					endTime: this.param.endDateStr + ' 23:59:59',
					pageNum: this.params.pageNum,
					pageSize: this.params.pageSize,
					merchantStoreIdArray: this.merchantStoreIdArray,
					secondaryType: this.secondaryType,
					commodityId: this.commodity.commodityId,
					dateType: this.inds,
					firstType: this.firstType
				}
				this.$api('commodityStockRecord.findCommodityStockRecordList', params).then(res => {
					if (res.status == 0) {
						if (res.data != null) {
							// 排行榜1
							this.list.push(...res.data.list);
							this.params.pageNum = res.data.pageNum;
							this.$nextTick(() => {
								this.$emit("countHeight");
							})
							if (this.params.pageNum >= res.data.pages) {
								this.loadmoreStatus = 'nomore';
								return;
							}
							// 新写的需要在这里加这个
							this.loadmoreStatus = 'loadmore';
						} else {
							this.params.pageNum = --this.params.pageNum;
							this.loadmoreStatus = 'loadmore';
							this.$helpers.toast('请求数据失败:暂未查到数据');
						}
					} else {
						this.params.pageNum = --this.params.pageNum;
						this.loadmoreStatus = 'loadmore';
						this.$helpers.toast('请求数据失败:' + res.msg);
					}
				}).catch(res => {
					this.params.pageNum = --this.params.pageNum;
					this.loadmoreStatus = 'loadmore';
					this.$helpers.toast('服务器访问失败');
				})
			},

			// 切换
			clickRouter(router) {
				this.router = router;
				if (router == 2) {
					this.initData();
				} else if (router == 1) {
					this.commodity = {};
				}
			},
			// 从子组件中获取到确认的开始和结束日期
			getConfirmDate(data) {
				console.log("data", data)
				this.param.startDateStr = data.startDateStr
				this.param.endDateStr = data.endDateStr
				if (this.router == 2) {
					uni.showLoading({
						mask: true,
						title: '加载中'
					})
					this.$nextTick(() => {
						this.$refs.vendingMachineListStats.reload();
					})
				}
				this.initData();
			},

			// 日期选择,今天,昨天,本月,上月,今年
			dateStrOpt(ind) {
				this.inds = ind;
				this.param.dateType = ind
				let time = this.$date.getTime();
				switch (this.param.dateType) {
					case 0:
						// 获取今天的日期
						this.param.startDateStr = this.$date.dateFormatYMD(new Date());
						this.param.endDateStr = this.$date.dateFormatYMD(new Date());
						break;
					case 1:
						// 获取昨天的日期
						this.param.startDateStr = this.$date.dateFormatYMD(time.startYesterday);
						this.param.endDateStr = this.$date.dateFormatYMD(time.startYesterday);
						break;
					case 2:
						// 获取本周的日期
						this.param.startDateStr = this.$date.dateFormatYMD(time.startWeek);
						this.param.endDateStr = this.$date.dateFormatYMD(new Date());
						break;
					case 3:
						// 获取本月的日期
						this.param.startDateStr = this.$date.dateFormatYMD(time.startThisMonth);
						this.param.endDateStr = this.$date.dateFormatYMD(new Date());
						break;
					case 4:
						// 获取今年的日期
						this.param.startDateStr = this.$date.dateFormatYMD(time.startThisYear);
						this.param.endDateStr = this.$date.dateFormatYMD(new Date());
						break;
					case 5:
						// 获取上月的日期
						this.param.startDateStr = this.$date.dateFormatYMD(time.startLastMonth);
						this.param.endDateStr = this.$date.dateFormatYMD(time.endLastMonth);
						break;
				}
				if (this.router == 2) {
					uni.showLoading({
						mask: true,
						title: '加载中'
					})
					this.$nextTick(() => {
						this.$refs.vendingMachineListStats.reload();
					})
				}
				this.initData();
			},
			setSiteList(data) {
				this.mchStoreIdArray = data.merchantStoreIdArray;
				console.log("数据",this.mchStoreIdArray)
				this.param.siteTitle = '已选中' + this.mchStoreIdArray.length + '个场地';
				this.initData();
			},
			showSiteTitle() {
				this.siteChecksShow = true;
			},
			closeSiteTitle() {
				this.siteChecksShow = false;
			},
			displaySwitch() {
				this.displayBoolen = !this.displayBoolen
			},
			rankingClick(item) {
				this.commodity = item;
				this.router = 2;
				this.initData();
			},
			closeIcon() {
				this.commodity = {};
				this.initData();
			}
		}
	}
</script>

<style lang="scss">
	/deep/ .u-tabs__wrapper__nav__line {
		border-radius: 0rpx !important;
	}

	page {
		background-color: #FFFFFF !important;
	}

	/deep/ .u-grid-item {
		margin-top: 20rpx !important;
	}

	/deep/.u-safe-area-inset-bottom {
		padding-bottom: 0 !important;
	}

	.main {
		.popup-title {
			color: rgba(0, 0, 0, 0.8);
			font-size: 32rpx;
			letter-spacing: 1rpx;
			text-align: center;
			margin-top: 50rpx;

			.moeny-title {
				color: rgb(0, 0, 0);
				font-size: 32rpx;
				font-weight: 700;
				letter-spacing: 1rpx;
				margin-left: 5rpx;
			}
		}

		.body {
			margin: 35rpx 50rpx 50rpx 50rpx;
			background: rgba(255, 144, 19, 0.2);
			border-radius: 0rpx 0rpx 10rpx 10rpx;

			.body-title {
				background: rgb(255, 215, 170);
				border-radius: 10rpx;
				color: rgba(0, 0, 0, 0.7);
				font-size: 24rpx;
				font-weight: 700;
				letter-spacing: 1rpx;
				padding: 20rpx 0rpx;
			}

			.body-content {
				color: rgba(0, 0, 0, 0.7);
				font-size: 24rpx;
				letter-spacing: 1rpx;
				//padding: 20rpx 0rpx;
				line-height: 40px;
			}
		}

		.top-warp {
			padding: 30rpx 24rpx;
			background-color: white;

			.card {
				border-radius: 20rpx;
				padding: 35rpx;
				display: flex;
				align-items: center;
				background: linear-gradient(270.00deg, rgba(96, 112, 255, 0.8), rgb(96, 112, 255) 100%);

				.card-left {
					flex: 1;
					display: flex;
					justify-content: space-between;

					.card-item {
						flex: 1;

						.item-title {
							color: rgba(255, 255, 255, 0.7);
							font-size: 22rpx;
						}

						.item-value {
							color: #fff;
							font-size: 46rpx;
							font-weight: 700;
							margin-top: 13rpx;
						}
					}
				}

				.card-right {
					display: flex;
					justify-content: flex-end;
					margin-left: 40rpx;

					.display {
						width: 52rpx;
						height: 52rpx;
						background: rgba(255, 255, 255, 0.16);
						border-radius: 50%;
						display: flex;
						justify-content: center;
						align-items: center;
					}
				}
			}
		}

		.business-hotspot {
			display: flex;
			justify-content: space-between;
			padding: 16rpx 20rpx;

			/* 选中的样式 */
			.tag-sel {
				font-size: $f-base;
				color: #303133;
				font-weight: bold;
			}

			/* 未选中的样式 */
			.tag {
				font-size: $f-base;
				color: rgba(0, 0, 0, 0.7);
			}

			.slider {
				width: 50rpx;
				height: 10rpx;
				background: #6072FF;
				transform: rotate(-0.40deg);
				margin: 15rpx 0 0 4rpx;
				border-radius: 10rpx;
			}
		}

		.header-sort-site-view {
			display: flex;
			justify-content: space-between;
			margin-top: 20rpx;
			padding: 0 20rpx;

			.header-sort-site {
				display: flex;

				.title {
					color: rgba(0, 0, 0, 0.7);
					font-size: $uni-font-size-base;
					letter-spacing: 0.5rpx;
					margin-right: 20rpx;
				}

				.header-sort-site-icon {
					margin-left: $u-margin-mini;
				}

				.a {
					color: #6072FF;
				}


			}
		}

		.ranking-item1 {
			// background-color: rgb(239, 238, 254);
			background-color: rgba(239, 238, 254, 0.4);
		}

		.ranking-item2 {
			// background-color: rgb(255, 236, 237);
			background-color: rgba(255, 236, 237, 0.4);
		}

		.ranking-item3 {
			background-color: rgba(250, 243, 234, 0.4);
			// background-color:  rgb(250, 243, 234);
		}

		.ranking-item-out {
			display: flex;
			align-items: center;
			padding: 20rpx 30rpx;
			padding-left: 0;
			border-radius: 20rpx;
			margin-top: 20rpx;
			position: relative;

			.close-icon {
				position: absolute;
				right: -10rpx;
				bottom: -10rpx;
				background-color: #FFFFFF;
				border-radius: 50%;
			}

			.item-limit {
				// padding: 0 20rpx;

				width: 90rpx;
				display: flex;
				align-items: center;
				justify-content: center;
			}

			.item-left {
				font-size: 28px;
			}

			.item-right {
				flex: 1;
				margin-left: 20rpx;
				display: flex;
				align-items: center;
				justify-content: space-between;

				.item-info {
					flex: 1;
					color: rgb(34, 34, 34);

					.item-name {
						font-size: 28rpx;
						overflow: hidden;
						-webkit-line-clamp: 2;
						text-overflow: ellipsis;
						display: -webkit-box;
						-webkit-box-orient: vertical;
					}

					.item-m {
						font-size: 24rpx;
						margin-top: 4rpx;
					}
				}

				.item-count {
					margin-left: 10rpx;
					color: rgb(102, 102, 102);
					font-size: 28rpx;
					width: 100rpx;
					text-align: right;
					// 强制换行
					word-wrap: break-word;
					// 强制英文单词换行
					word-break: break-word;
				}
			}
		}

		.image-32 {
			width: 32rpx;
			height: 32rpx;
		}

		.coin {
			text-align: right;
			color: rgba(0, 0, 0, 0.7);
			font-size: 32rpx;
		}

		.store-device {
			margin-top: 15rpx;
			color: rgb(120, 121, 123);
			font-size: 24rpx;
		}

		.avatar {
			position: relative;

			// top:10rpx;
			.girl {
				position: absolute;
				right: 0;
				top: 56rpx;
			}
		}

		.card-list {
			background: rgb(250, 250, 250);
			border-radius: 20rpx;
			padding: 26rpx $u-padding-large;
			margin: 26rpx;
			font-size: $f-base;

			.device-info {
				color: rgb(120, 121, 123);
				font-size: 26rpx;
				letter-spacing: 1rpx;
				margin-top: 10rpx;
			}

			.title {
				color: rgba(0, 0, 0, 0.9);
				font-size: 32rpx;
				margin-left: 10rpx;
			}

			.shuoming {
				display: flex;
				justify-content: space-between;
				font-size: $uni-font-size-base;
				letter-spacing: 0.5rpx;

				.title {
					width: 450rpx;
					color: rgba(0, 0, 0, 0.7);
				}

				.yuan {
					color: rgba(0, 0, 0, 0.9);
				}
			}

			.time {
				color: rgba(0, 0, 0, 0.5);
				margin-top: 30rpx;
			}
		}
	}

	// 不知道为何底部安全距离没生效, 这里手动给一个
	/deep/ .u-safe-area-inset-bottom {
		padding-bottom: 34px !important;
	}
</style>
